package com.tao.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tao.entity.User;
import com.tao.service.UserService;

/**
 * 用户的控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/servlet/UserServlet")
public class UserController {
	@Autowired
	private UserService userService;
	
	//通过手机号来新增用户
	
	@RequestMapping(value={"/AddUserByPhone"},
			        params={"phone","pwd"},
			        method=RequestMethod.POST)
	@ResponseBody
	public int  AddUserByPhone(
			@RequestParam(value="phone") String phone,
			@RequestParam(value="pwd") String pwd){
		int s = 0;
		List<User> list = userService.findByPhone(phone);
		System.out.print("lalalal" + list.size());
		if (list.size() > 0) {
			s = -1; // 电话号码重复
		} else {
			User user = new User();
			user.setAccount(phone);
			user.setPhone(phone);
			user.setPwd(pwd);
			boolean flag = userService.addUserByPhone(user);
			if (flag) {
				s = 1; // 新增成功
			} else {
				s = 0; // 新增失败
			}
		}
		return s;
	}
	
	//通过QQ来新增用户
		@RequestMapping(value={"/AddUserByQQ"},
				        params={"qq"},
				        method=RequestMethod.POST)
		@ResponseBody
		public int  AddUserByQQ(
				@RequestParam(value="qq") String qq){
			int s=0;
			List<User> list=userService.findByQq(qq);
			if (list.size() > 0) {
				s = -1; // qq已经存在于数据库中
			} else {
			 User user=new User();
			 user.setQq(qq);
			 boolean flag=userService.addUserByQQ(user);
			 if(flag){
				 s=1;  //新增成功
			 }else{
				 s=0;  //新增失败
			 }
			}
			return s;
		}

}
